const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('src/cyc.db')
import sqlOption from '@/apis/sqlOption'
class sqlService {
  getTableList() {
    return new Promise((resolve, reject) => {
      db.serialize(() => {
        db.all(sqlOption.allTableSql(), (err, rows) => {
          if (err) {
            reject(err)
            return console.error(err.message);
          } else {
            resolve(rows)
          }
        })
      })
    })
  }

  // 数据结构
  checkData(model) {
    return new Promise((resolve, reject) => {
      db.serialize(() => {
        db.all(sqlOption.infoSql(model), (err, rows) => {
          if (err) {
            reject(err)
            throw err;
          } else {
            resolve(rows)
          }
        });
      });
    });
  }

  // 获取全部数据
  getAllData(table) {
    return new Promise((resolve, reject) => {
      let sql = sqlOption.allDataSql(table)
      db.serialize(() => {
        db.all(sql, (err, rows) => {
          if (err) {
            reject(err)
            throw err;
          } else {
            resolve({
              sql,
              code: 1000,
              total: rows.length,
              data: rows
            })
          }
        });
      });
    });
  }

  // 属性列表
  getColumnList(table) {
    return new Promise((resolve, reject) => {
      let sql = sqlOption.columnsSql(table)
      db.serialize(() => {
        db.all(sql, (err, rows) => {
          if (err) {
            reject(err)
            return console.error(err.message);
          } else {
            resolve({
              sql,
              code: 1000,
              total: rows.length,
              data: rows
            })
          }
        })
      })
    })
  }

  // 查找数据
  queryData(model) {
    return new Promise((resolve, reject) => {
      let sqlObj = sqlOption.querySql(model)
      db.serialize(() => {
        db.all(sqlObj.all, (err, rows) => {
          if (!err) {
            db.all(sqlObj.sql, (err2, rows2) => {
              if (!err2) {
                resolve({
                  sql: sqlObj.sql,
                  code: 1000,
                  total: rows.length,
                  data: rows2
                })
              } else {
                reject(err2)
              }
            })
          } else {
            reject(err)
          }
        })
      })
    })
  }

  // 添加数据
  addData(model) {
    return new Promise((resolve, reject) => {
      const sql = sqlOption.addSql(model)
      db.serialize(() => {
        db.all(sql, (err, rows) => {
          if (!err) {
            if (rows.length == 0) {
              resolve({
                code: 1000,
                msg: '添加成功'
              })
            }
          } else {
            reject(err)
          }
        })
      })
    })
  }

  // 编辑数据
  editData(model) {
    return new Promise((resolve, reject) => {
      const sql = sqlOption.editSql(model)
      console.log(sql)
      db.serialize(() => {
        db.all(sql, (err, rows) => {
          if (!err) {
            if (rows.length == 0) {
              resolve({
                sql,
                code: 1000,
                data: model.data,
                msg: '编辑成功'
              })
            }
          } else {
            reject(err)
          }
        })
      })
    })
  }

  // 删除数据
  delData(table, id) {
    return new Promise((resolve, reject) => {
      const sql = sqlOption.delSql(model)
      db.serialize(() => {
        db.all(sql, (err, rows) => {
          if (!err) {
            if (rows.length == 0) {
              resolve({
                code: 1001,
                msg: '删除成功'
              })
            }
          } else {
            reject(err)
          }
        })
      })
    })
  }

  // 批量排序
  batchSort(model) {
    return new Promise((resolve, reject) => {
      const sql = sqlOption.batchSortSql(model)
      db.serialize(() => {
        db.all(sql, (err, rows) => {
          if (!err) {
            if (rows.length == 0) {
              resolve({
                code: 1000,
                sql,
                msg: '操作成功'
              })
            }
          } else {
            reject(err)
          }
        })
      })
    })
  }

  // 树形排序
  treeSort(model) {
    return new Promise((resolve, reject) => {
      const sql = sqlOption.treeSortSql(model)
      db.serialize(() => {
        db.all(sql, (err, rows) => {
          if (!err) {
            if (rows.length == 0) {
              resolve({
                code: 1000,
                sql,
                msg: '操作成功'
              })
            }
          } else {
            reject(err)
          }
        })
      })
    })
  }

}

export default new sqlService()
